/**
 * sms_toy
 * 2012-3-1 2012
 * root
 * Copyright (c) 2012 Alex Wang
 */
package com.joyo8.app;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.location.Location;
import android.os.IBinder;
import android.sax.StartElementListener;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;

/**
 * @author root
 *
 */
public class SmsRecerver extends BroadcastReceiver {

    /* (non-Javadoc)
     * @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)
     */
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        String _action = intent.getAction();
        Constants.mUtil.logd(context.getApplicationInfo().className + " receive a intent: "+ _action);
        if(Constants.INTENT_SMS.equals(_action)){
            Constants.mUtil.logd(context.getApplicationInfo().className + " received a new sms ");
            Object[] pdus = (Object[]) intent.getExtras().get("pdus");// get sms
            for (Object pdu : pdus) {
                byte[] data = (byte[]) pdu;
                // use pdu format sms to get message
                SmsMessage message = SmsMessage.createFromPdu(data);
                // get sms sender
                String sender = message.getOriginatingAddress();
                String content = message.getMessageBody();
                 Constants.mUtil.logd("the sms sender:"+sender);
                // if set sms speech on
                if (Constants.mUtil.getSpeechSMSState(context)) {
                    ComponentName comp = new ComponentName(context.getPackageName(),
                            SmsToSpeech.class.getName());
                    context.startActivity(new Intent().setComponent(comp).putExtra(
                            Constants.EXTRA_SMS_CONTENT, content).putExtra(
                            Constants.EXTRA_SMS_SENDER, sender).addFlags(
                            Intent.FLAG_ACTIVITY_NEW_TASK));
                }
                
                /* sms content recognize */
                /*if(content.indexOf("你在哪儿")>-1){
                    Location loc = Constants.mUtil.getLoction(context);
                    String location = "对方的位置:"+loc.getLatitude()+" | "+ loc.getLongitude();
                     SmsManager manager = SmsManager.getDefault();
                      manager.sendTextMessage(sender,
                      message.getServiceCenterAddress(), location, null,
                      null);
                     
                }else if(content.indexOf("对方的位置:")>-1){
                    //TODO start view map app
                }*/
            }
            
        }else if(Intent.ACTION_PACKAGE_ADDED.equals(_action)){
            Constants.mUtil.logd("start service");
            Intent in = new Intent();
            intent.setClass(context, DXService.class);
            context.startService(intent);
        }else if(Intent.ACTION_BOOT_COMPLETED.equals(_action)){
            Intent intent2 = new Intent();
            intent2.setClass(context, DXService.class);
            context.startService(intent2);
        }

    }
    
    

}
